package com.game.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.game.common.Result;
import com.game.model.Room;
import com.game.service.RoomService;
import com.game.vo.RoomStatusVO;
import com.game.vo.RoomVO;
import com.game.vo.request.PlayCardsRequest;
import com.game.vo.request.SubmitHandRequest;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/rooms")
@RequiredArgsConstructor
public class RoomController {
    private final RoomService roomService;
    private static final Logger log = LoggerFactory.getLogger(RoomController.class);

    @PostMapping("/create")
    public Result<RoomVO> createRoom(@RequestAttribute("userId") Long userId) {
        return Result.success(roomService.createRoom(userId));
    }

    @GetMapping("/list")
    public Result<List<RoomVO>> getAvailableRooms() {
        return Result.success(roomService.getAvailableRooms());
    }

    @GetMapping("/{roomCode}")
    public Result<Room> getRoom(@PathVariable String roomCode) {
        return Result.success(roomService.getRoom(roomCode));
    }

    @GetMapping("/{roomCode}/status")
    public Result<RoomStatusVO> getRoomStatus(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId) {
        return Result.success(roomService.getRoomStatus(roomCode, userId));
    }

    @PostMapping("/{roomCode}/join")
    public Result<Void> joinRoom(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId) {
        roomService.joinRoom(roomCode, userId);
        return Result.success();
    }

    @PostMapping("/{roomCode}/leave")
    public Result<Void> leaveRoom(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId) {
        roomService.leaveRoom(roomCode, userId);
        return Result.success();
    }

    @PostMapping("/{roomCode}/ready")
    public Result<Void> ready(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId) {
        roomService.ready(roomCode, userId);
        return Result.success();
    }

    @PostMapping("/{roomCode}/cancel-ready")
    public Result<Void> cancelReady(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId) {
        roomService.cancelReady(roomCode, userId);
        return Result.success();
    }

    @PostMapping("/{roomCode}/play")
    public Result<Void> playCards(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId,
            @RequestBody PlayCardsRequest request) {
        roomService.playCards(roomCode, userId, request.getCards());
        return Result.success();
    }

    @PostMapping("/{roomCode}/pass")
    public Result<Void> pass(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId) {
        roomService.pass(roomCode, userId);
        return Result.success();
    }

    @PostMapping("/{roomCode}/start")
    public Result<Void> startGame(@PathVariable String roomCode, @RequestAttribute("userId") Long userId) {
        roomService.startGame(roomCode, userId);
        return Result.success();
    }

    @DeleteMapping("/{roomCode}")
    public Result<Void> deleteRoom(@PathVariable String roomCode) {
        roomService.deleteRoom(roomCode);
        return Result.success();
    }
    
    @PostMapping("/{roomCode}/submit-hand")
    public Result<Void> submitHand(
            @PathVariable String roomCode,
            @RequestAttribute("userId") Long userId,
            @RequestBody SubmitHandRequest request) {
        log.info("用户 {} 提交牌型，房间号: {}, 前墩: {}, 中墩: {}, 后墩: {}", 
                 userId, roomCode, request.getFrontHand(), request.getMiddleHand(), request.getBackHand());
        roomService.submitHand(roomCode, userId, request.getFrontHand(), request.getMiddleHand(), request.getBackHand());
        return Result.success();
    }
} 